package com.ruoyi.sl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sl.domain.SlApprentice;
import com.ruoyi.sl.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sl.domain.SlSa;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 水产小时系数Controller
 * 
 * @author admin
 * @date 2024-06-12
 */
@RestController
@RequestMapping("/sl/sa")
public class SlSaController extends BaseController
{
    @Autowired
    private ISlSaService slSaService;

    @Autowired
    private ISlLevelService slLevelService;

    @Autowired
    private ISlModuleService slModuleService;

    @Autowired
    private ISlWorkshopService slWorkshopService;

    @Autowired
    private ISlGprocessService slGprocessService;
    /**
     * 查询水产小时系数列表
     */
    @PreAuthorize("@ss.hasPermi('sl:sa:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlSa slSa)
    {
        startPage();
        List<SlSa> list = slSaService.selectSlSaList(slSa);
        return getDataTable(list);
    }

    /**
     * 导出水产小时系数列表
     */
    @PreAuthorize("@ss.hasPermi('sl:sa:export')")
    @Log(title = "水产小时系数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlSa slSa)
    {
        List<SlSa> list = slSaService.selectSlSaList(slSa);
        ExcelUtil<SlSa> util = new ExcelUtil<SlSa>(SlSa.class);
        util.exportExcel(response, list, "水产小时系数数据");
    }

    /**
     * 获取水产小时系数详细信息
     */
    @PreAuthorize("@ss.hasPermi('sl:sa:query')")
    @GetMapping(value = { "/", "/{saId}" })
    public AjaxResult getInfo(@PathVariable(value = "saId", required = false) Long saId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("modules", slModuleService.selectModuleAll());
        ajax.put("levels", slLevelService.selectLevelAll());
        //水产模块
        ajax.put("workshops", slWorkshopService.selectSlWorkshopListByModuleId(3L));
        if (StringUtils.isNotNull(saId))
        {
            SlSa slSa = slSaService.selectSlSaBySaId(saId);
            ajax.put(AjaxResult.DATA_TAG, slSa);
            //获取车间对应的大工序
            if(StringUtils.isNotNull(slSa.getWorkshopId())){
                ajax.put("gprocesss", slGprocessService.selectSlGprocessListByWorkshopId(slSa.getWorkshopId()));
            }
        }
        return ajax;
        //return success(slSaService.selectSlSaBySaId(saId));
    }

    /**
     * 新增水产小时系数
     */
    @PreAuthorize("@ss.hasPermi('sl:sa:add')")
    @Log(title = "水产小时系数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlSa slSa)
    {
        if (!slSaService.checkGprocessIdUnique(slSa))
        {
            return error("该大工序对应的数据已存在，添加失败。");
        }
        slSa.setCreateBy(getUsername());
        return toAjax(slSaService.insertSlSa(slSa));
    }

    /**
     * 修改水产小时系数
     */
    @PreAuthorize("@ss.hasPermi('sl:sa:edit')")
    @Log(title = "水产小时系数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlSa slSa)
    {
        if (!slSaService.checkGprocessIdUnique(slSa))
        {
            return error("该大工序对应的数据已存在，修改失败。");
        }
        slSa.setUpdateBy(getUsername());
        return toAjax(slSaService.updateSlSa(slSa));
    }

    /**
     * 删除水产小时系数
     */
    @PreAuthorize("@ss.hasPermi('sl:sa:remove')")
    @Log(title = "水产小时系数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{saIds}")
    public AjaxResult remove(@PathVariable Long[] saIds)
    {
        return toAjax(slSaService.deleteSlSaBySaIds(saIds));
    }
}
